<%@page import="java.io.PrintWriter"%>
<%@page import="java.sql.ResultSet" import="java.sql.Statement" import="java.sql.Connection" import="connection.DBConnection"%>
<%@page import="java.util.*" %>
<%@page import="java.text.SimpleDateFormat" %> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sales Voucher</title>
 
<script language="javascript" type="text/javascript"> 

function checkDate()
{
	var c1;
	
  	var datevalid=/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
  		
        c1= document.f14.rdate.value;

        if(c1.length==0)
        {
        	alert("Enter Date");
            return false;
      	 }
        if(!c1.match(datevalid))
        {
            alert("Please Enter Date in DD/MM/YYYY Format");
            document.f14.rdate.value="";
            document.f14.rdate.focus();
            
	         return false;
    	 }
        else
        {
        	dateval=c1.match(datevalid);
        }
        
        var dtYear = dateval[1];        
        dtMonth = dateval[3];
        dtDay=  dateval[5];

     if (dtMonth < 1 || dtMonth > 12) 
        return false;
     else if (dtDay < 1 || dtDay> 31) 
       return false;
     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
       return false;
     else if (dtMonth == 2){ 
       var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
       if (dtDay> 29 || (dtDay ==29 && !isleap)) 
      	 
      	 alert("Date format is not correct");
       document.f14.rdate.value="";
       document.f14.rdate.focus();
              return false;
        
        return true;
}

</script>

<script language="javascript" type="text/javascript"> 

function populateCustomerId(){
    var custname = document.getElementById('custname');

    /* selected value of dropdown */
    var selectedCustomerId = custname.options[custname.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('custcode').value = selectedCustomerId; 
}
     
</script>


<script language="javascript" type="text/javascript">

function NetAmount()
{
    var p1 = document.getElementById("prodquantity").value;
    var p2 = document.getElementById("prodrate").value;
   
    var p3=parseInt(p1) * parseFloat(p2);
 
    document.getElementById("netamount").value = p3; 
}
    
</script>   


<script language="javascript" type="text/javascript">

function TaxAmount(opt)
{
	var tax1 = document.getElementById("netamount").value;
	var tax2= opt.text;
	
	var nettax= (tax1 * tax2) /100;
	document.getElementById("taxamount").value = nettax;	
}
</script>

<script language="javascript" type="text/javascript">

function TotalAmount()
{
	var t1 = document.getElementById("netamount").value;
	var t2 = document.getElementById("taxamount").value;
	var t3 = document.getElementById("transport").value;
	
	var total=parseFloat(t1)+parseFloat(t2)+parseFloat(t3);
	
	document.getElementById("totalamount").value = total;

}
	
</script>

<script language="javascript" type="text/javascript">
    
    function setAction(var action) 
    {
        document.getElementById('action').value = action;
    }
</script>

</head>
<body>
<form name="f11"  action="EditSalesVoucherServlet" ONSUBMIT="return checkDate()"> 

<% 		String selectedaccnumber = (String) request.getAttribute("vnumber");


		DBConnection dbc=new DBConnection();	
		Connection con=dbc.getNewConnection();

		Statement st = null;
		ResultSet rs = null;
		String code2="";
		
		String s1="",s2="",s3="",s4="",s5="",s14="";
		int s6=0,s13=0;
		double s7=0.0,s8=0.0,s9=0.0,s10=0.0,s11=0.0,s12=0.0;
		
	
		try
		{
			st=con.createStatement() ;
			rs=st.executeQuery("select * from SalesVoucher2 where innum='"+selectedaccnumber+"' "); %>
  
			<%  while(rs.next()) 
				{ 
					s1=rs.getString(1);
					s2=rs.getString(2);					
					s3=rs.getString(3);
					s4=rs.getString(4);
					s5=rs.getString(5);
					s6=rs.getInt(6);
					s7=rs.getDouble(7);
					s8=rs.getDouble(8);
					s9=rs.getDouble(9);
					s10=rs.getDouble(10);
					s11=rs.getDouble(11);
					s12=rs.getDouble(12);
					s13=rs.getInt(13);
					s14=rs.getString(14);
				}  
		 }
		catch(Exception e){} 		
%>

<table cellspacing=3>
<tr>
	<td> Against Sales order number </td>
	<td> <input type="text" name="ordnum"/> </td>
<tr>
	<td>Invoice number</td>		   
	<td><input type="text" name="Innum" value="<%=s1 %>"/></td>
	
	<% 	
	     SimpleDateFormat fromsql = new SimpleDateFormat("yyyy-MM-dd");
		 SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		 String v2 =formatter.format(fromsql.parse(s2));

	%>
	
	<td>Date</td>
	<td><input type="text" name="dte" value="<%=v2 %>" ></td>
	
</tr>

<tr>
	<td>Name of Customer</td>
	<td><input type="text" name="custname" value="<%=s3 %>"> </td> 
		
<%-- 		<%	
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select ccode,cname from CustomerMaster"); %>
			
			<td> 
	  			<select name="custname" id="custname" onchange="populateCustomerId();">
	  				<option> </option>
       		 		<%  while(rs.next()){ %>
            			<option value="<%=rs.getString(1)%>" ><%= rs.getString(2)%></option>
      
     			    <% } %>
      			</select>
    		</td>
  
			<% }
			catch(Exception e){} %>
--%>	
	 <td><a href="CustomerMaster.jsp"> Create New Customer </a></td>
	<!-- <td><input type=button onClick=window.open("CustomerMaster.jsp","Ratting","width=550, height=170,left=150,top=200,toolbar=0,status=0,"); value="Open Window"></td> -->
	<td><input type="submit" name="act" value="Refresh account names"/>
</tr>

<tr>
	<td>Customer code</td>
	<td><input type="text" name="custcode" id="custcode" value="<%=s4 %>" ></td>
	<td>Customer Invoice number</td>
	<td><input type="text" name="custinnum" /> </td>
	</tr>
	
<tr>
	<td>Product Name</td>
	<td><input type="text" name="prodname" value="<%=s5 %>" ></td> 
		
<%--	 <%	 
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select stname from StockMaster"); %>
	<td> 
	  <select name="prodname">
	  	<option> </option>
       		 <%  while(rs.next()){ %>
            		<option><%= rs.getString(1)%></option>
      
          <% } %>
       </select>
     </td>
  
	 <% }
		catch(Exception e){} %>
--%>	

	<td><a href="StockMaster.jsp"> Create New Stock </a></td>
	<td><input type="submit" name="act" value="Refresh Item names"/></td>
	</tr>
	
<tr>
	<td>Product Quantity</td>
	<td><input type="text" name="prodquantity" id="prodquantity" value="<%=s6 %>" ></td>
	
	<td>Product Rate</td>
	<td><input type="text" name="prodrate" id="prodrate" value="<%=s7 %>" > </td>
</tr>

<tr> 
	<td>Net Amount</td>
	<td><input type="text" name="netamount" id="netamount" value="<%=s8%>" /></td>
	
	<td><input type="button" value="Calculate Net Amount"  onClick="NetAmount();"/> </td>
	
</tr>
<tr>
	<td>Taxation Category</td>
	<td> <input type="text" name="taxcat" value="<%=s9 %>" >
<%--	<select name="taxcat" id="taxcat" onchange="TaxAmount((this.options[this.selectedIndex]));" >
	<option> </option>
	<option value="5.0"> 5.0 </option>
	<option value="12.5"> 12.5 </option>
	</select>
	--%>
	
	</td>
</tr>
	
<tr>
	<td>Tax Amount</td>
	<td><input type="text" name="taxamount" id="taxamount" value="<%=s10 %>" ></td>
	<td><input type="button" name="act" value="Calculate Tax Amount" /></td>
</tr>
	
<tr>
	<td>Transport Charges</td>
	<td> <input type="text" name="transport" id="transport" value="<%=s11 %>" ></td>
</tr>
	
<tr>
	<td>Total Amount</td>
	<td><input type="text" name="totalamount" id="totalamount" value="<%=s12 %>" ></td>
	<td><input type="button" name="act" value="Calculate Total Amount" onClick="TotalAmount();" /></td>
</tr>
	
<tr>
	<td>Credit limit (No. of Days)</td>
	<td><input type="text" name="credlimit" value="<%=s13 %>" > </td>
	</tr>
<tr>
	<td><input type="submit" name="update" value="Update" /></td>
</tr>
</table>
</form>

<form action="SalesVoucherDeleteServlet">
 <input type="submit" value="Delete">
 <input type=hidden name="delete" value="<%=s1 %>">
</form>

</body>
</html>